adding (unused) loss field to BablFormat, simplification of statistical calculations...
authorØyvind Kolås <ok@src.gnome.org>
Sun, 11 Sep 2005 22:45:46 +0000 (22:45 +0000)
committerØyvind Kolås <ok@src.gnome.org>
Sun, 11 Sep 2005 22:45:46 +0000 (22:45 +0000)
ChangeLog
babl/babl-classes.h
babl/babl-format.c
tests/conversions.c
tests/formats.c

index 10e585348fe82890a71da8ba820d05687094f61a..1fd7368e4080a9fceec359c514d5f359bcf57655 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2005-09-12  Øyvind Kolås  <pippin@gimp.org>
+
+       * babl/babl-classes.h: add loss field to format class.
+       * babl/babl-format.c: (format_new): set loss field to -1 as initial
+       value.
+       * tests/conversions.c: (reference_format): simplify
+       * tests/formats.c: (test_init), (format_check), (main): code
+       simplification.
+
 2005-09-11  Øyvind Kolås  <pippin@gimp.org>
 
        * babl/babl.h: use gcc 4.x attribute for NULL sentinel.
index 7bdd4f9985bbb75ca87d65085651ceae7ca56dfd..d6adbb9f166eab6b1464523db12a032944ac41e0 100644 (file)
@@ -184,6 +184,8 @@ typedef struct
   BablModel       *model;
   int              bytes_per_pixel;
   int              planar;
+  double           loss; /*< average relative error when converting 
+                             from and to RGBA double */
 } BablFormat;
 
 typedef struct
index f461639b6256576d96403e620cdf0cea1f5dbf84..734932d13bdf815285fcfb8e08c7f4068bb93cb1 100644 (file)
@@ -100,6 +100,8 @@ format_new (const char     *name,
       babl->format.bytes_per_pixel += type[i]->bits/8;
   }
 
+  babl->format.loss = -1.0;
+
   return babl;
 }
 
index 6c1ba8dba97cfb4c75324a52ea74dd276535da9c..c2abf1bbef0bf9dd54b18badf7e4c473358120a3 100644 (file)
@@ -51,10 +51,7 @@ void test_init (void)
 
 static Babl *reference_format (void)
 {
-  static Babl *self = NULL;
-  
-  if (!self)
-     self = babl_format_new (
+  return babl_format_new (
        babl_model ("RGBA"),
        babl_type ("double"),
        babl_component ("R"),
@@ -62,7 +59,6 @@ static Babl *reference_format (void)
        babl_component ("B"),
        babl_component ("A"),
        NULL);
-  return self;
 }
 
 static void
index b5f31466335e6f47b5c7ed54cc9ad7d7d7639ff1..721ccc51b574fb1a04d3d16cda755bd2ee1ab3f8 100644 (file)
@@ -5,63 +5,17 @@
 #include <math.h>
 #include "babl-internal.h"
 
-int OK=1;
+#define pixels  1024
 
-#define pixels    1024
-#define TOLERANCE 0.001
+static double test[pixels * 4];
 
-double test[pixels * 4];
-
-double r_interval (double min, double max)
+static void
+test_init (void)
 {
-  long int rand_i = random ();
-  double ret;
-  ret = (double) rand_i / RAND_MAX;
-  ret*=(max-min);
-  ret+=min;
-  return ret;
-}
+  int    i;
 
-void test_init (void)
-{
-  double r_min  =  0.0,
-         r_max  =  1.0,
-         g_min  =  0.0,
-         g_max  =  1.0,
-         b_min  =  0.0,
-         b_max  =  1.0,
-         a_min  =  0.0,
-         a_max  =  1.0;
-  int i;
-  double r,g,b,a;
-  for (i=0;i<pixels;i++)
-    {
-      r=r_interval(r_min, r_max);
-      g=r_interval(g_min, g_max);
-      b=r_interval(b_min, b_max);
-      a=r_interval(a_min, a_max);
-      test [i*4 + 0]=r;
-      test [i*4 + 1]=g;
-      test [i*4 + 2]=b;
-      test [i*4 + 3]=a;
-    }
-}
-
-
-static Babl *reference_format (void)
-{
-  static Babl *self = NULL;
-  
-  if (!self)
-     self = babl_format_new (
-       babl_model ("RGBA"),
-       babl_type ("double"),
-       babl_component ("R"),
-       babl_component ("G"),
-       babl_component ("B"),
-       babl_component ("A"),
-       NULL);
-  return self;
+  for (i = 0; i < pixels * 4; i++)
+     test [i] = (double)random () / RAND_MAX;
 }
 
 int format_check (Babl *babl,
@@ -77,8 +31,16 @@ int format_check (Babl *babl,
   Babl *fish_to;
   Babl *fish_from;
 
-  ref_fmt   = reference_format ();
-  fmt       = babl; /*construct_double_format (babl);*/
+  ref_fmt   = babl_format_new (
+       babl_model ("RGBA"),
+       babl_type ("double"),
+       babl_component ("R"),
+       babl_component ("G"),
+       babl_component ("B"),
+       babl_component ("A"),
+       NULL);
+
+  fmt       = babl; 
   fish_to   = babl_fish (ref_fmt, fmt);
   fish_from = babl_fish (fmt, ref_fmt);
   
@@ -94,47 +56,17 @@ int format_check (Babl *babl,
 
   {
     int i;
-    int log=0;
     double loss=0.0;
 
-    for (i=0;i<pixels;i++)
+    for (i=0;i<pixels*4;i++)
       {
-        int j;
-        for (j=0;j<4;j++)
-          {
-           loss += fabs (clipped[i*4+j] - test[i*4+j]);
-
-           if (fabs (clipped[i*4+j] - transformed[i*4+j])>TOLERANCE)
-             {
-                if (!log)
-                  log=1;
-                OK=0;
-             }
-          }
-        if (log && log < 5)
-          {
-            babl_log ("%s", babl->instance.name);
-            babl_log ("\ttest:     %2.3f %2.3f %2.3f %2.3f", test [i*4+0],
-                                                             test [i*4+1],
-                                                             test [i*4+2],
-                                                             test [i*4+3]);
-            babl_log ("\tclipped:  %2.3f %2.3f %2.3f %2.3f", clipped [i*4+0],
-                                                             clipped [i*4+1],
-                                                             clipped [i*4+2],
-                                                             clipped [i*4+3]);
-            babl_log ("\ttrnsfrmd: %2.3f %2.3f %2.3f %2.3f", transformed [i*4+0],
-                                                             transformed [i*4+1],
-                                                             transformed [i*4+2],
-                                                             transformed [i*4+3]);
-            log++;
-            OK=0;
-          }
+        loss += fabs (clipped[i] - test[i]);
       }
     loss /= pixels;
-       {
-          babl_log ("%s\tloss:%f%%", babl->instance.name, loss * 100.0);
-          OK = 0;
-       }
+
+    if (userdata)
+      babl_log ("%s\tloss: %f", babl->instance.name, loss);
+    babl->format.loss = loss;
   }
   
   babl_free (original);
@@ -150,9 +82,9 @@ int main (void)
   test_init ();
 
   babl_set_extender (babl_extension_quiet_log ());
-  babl_format_each (format_check, NULL);
+  babl_format_each (format_check, (void*)1);
 
   babl_destroy ();
 
-  return !OK;
+  return 0;
 }